package com.fl.nettydemo.c1;

import com.fl.nettydemo.handler.ServerHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class HelloServier {
    public static void main(String[] args) {
        //1.启动器 负责组装netty组件 并启动服务
        new ServerBootstrap()
                //2. 添加一个循环事件线程组
                .group(new NioEventLoopGroup())
                //3. 选择一个服务链接通道类型
                .channel(NioServerSocketChannel.class)
                //4. 负责处理链接 读写操作
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    //5.channel服务器读写通道
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        // 处理buffer
                       // pipeline.addLast(new StringDecoder());
                        pipeline.addLast("IdleStateHandler",new IdleStateHandler(20, 0, 0, TimeUnit.SECONDS));
                        // 自定义处理事件
                        pipeline.addLast("ServerHandler",new ServerHandler());
                    }
                }).bind(1000);

    }
}
